package com.jun.seckill_by_jun.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取单个对象
     * @param keyPrefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPrefix() + key;
            String str = jedis.get(realKey);
            T t = stringToBean(str,clazz);
            return t;
        }finally{
            returnToPool(jedis);
        }
    }

    /**
     * 设置对象
     * @param keyPrefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> Boolean set(KeyPrefix keyPrefix,String key,T value){
        Jedis jedis =null;
        String realKey = keyPrefix.getPrefix() + key;
        try{
            jedis = jedisPool.getResource();
            String str = beanToString(value);
            if(str == null || str.length() == 0){
                return false;
            }
            if(keyPrefix.expireSeconds() <= 0){
                jedis.set(realKey,str);
            }else{
                jedis.setex(realKey,keyPrefix.expireSeconds(),str);
            }
            return true;
        }finally{
            returnToPool(jedis);
        }
    }

    /**
     * 判断是否存在
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> boolean exists(KeyPrefix keyPrefix,String key){
        Jedis jedis =null;
        String realKey = keyPrefix.getPrefix() + key;
        try{
            jedis = jedisPool.getResource();
            return jedis.exists(realKey);
        }finally{
            returnToPool(jedis);
        }
    }

    /**
     * 增加值
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long incr(KeyPrefix keyPrefix,String key){
        Jedis jedis =null;
        String realKey = keyPrefix.getPrefix() + key;
        try{
            jedis = jedisPool.getResource();
            return jedis.incr(realKey);
        }finally{
            returnToPool(jedis);
        }
    }

    /**
     * 减小值
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long decr(KeyPrefix keyPrefix,String key){
        Jedis jedis =null;
        String realKey = keyPrefix.getPrefix() + key;
        try{
            jedis = jedisPool.getResource();
            return jedis.decr(realKey);
        }finally{
            returnToPool(jedis);
        }
    }

    public boolean delete(KeyPrefix keyPrefix,String key){
        Jedis jedis =null;
        String realKey = keyPrefix.getPrefix() + key;
        try{
            jedis = jedisPool.getResource();
            long ret =  jedis.del(realKey);
            return ret > 0;
        }finally{
            returnToPool(jedis);
        }
    }

    public static <T> String beanToString(T value) {
        if(value == null){
            return null;
        }
        Class<?> clazz = value.getClass();
        if(clazz == int.class||clazz==Integer.class){
            return ""+value;
        }else if(clazz == String.class){
            return (String) value;
        }else if(clazz == long.class || clazz ==Long.class){
            return ""+value;
        }
        return JSON.toJSONString(value);
    }

    public static <T> T stringToBean(String str,Class<T> clazz) {
        if(str == null|| str.length() <=0||clazz == null){
            return null;
        }
        if(clazz == int.class||clazz == Integer.class){
            return (T) Integer.valueOf(str);
        }else if(clazz == long.class|| clazz == Long.class){
            return (T) Long.valueOf(str);
        }else if(clazz == String.class){
            return (T) str;
        }else{
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }

    private void returnToPool(Jedis jedis){
        if(jedis != null){
            jedis.close();
        }
    }

}
